import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.processUrl + '/v1'
const basePlanUrl = CommonConfig.planningUrl + '/v1'

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
// material/list
// 分页获取物料信息
export function getMaterialList ({ keyword, searchBy, materialProperty, materialType, pageNum, pageSize }) {
  const newUrl = `${baseUrl}/material/list`
  let reqData = {
    params: {
      materialType: materialType === 'all' ? '' : materialType,
      pageNum,
      pageSize,
      keyword,
      searchBy,
      materialProperty: materialProperty === 'all' ? '' : materialProperty
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 单个删除
export function moldDelete (id) {
  const newUrl = `${baseUrl}/mold/${id}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// 批量删除\DELETE
export function materialDeleteBatch (id) {
  const newUrl = `${baseUrl}/material`
  let reqData = {
    params: {
      id
    },
    paramsSerializer: function (id) {
      return qs.stringify(id, qsConfig)
    }
  }
  return axiosInstance.delete(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 保存物料信息
export function saveMaterial (params) {
  const newUrl = baseUrl + '/material'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// PUT
// /v1/material/{id}
// 更新单个物料信息
export function getUpdateMaterial (params) {
  const newUrl = baseUrl + `/material/${params.id}`
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// GET
// /v1/mold/model/list
// 按照模具编号或名称查询信息
export function getDictModelList ({ searchBy, keyword }) {
  const newUrl = `${baseUrl}/mold/model/list`
  let reqData = {
    params: {
      searchBy,
      keyword
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// /monitor/binding/byMoldNo获取当前模具厂是否已经装载
export function getShowLibrary (serialNumber) {
  const newUrl = `${baseUrl}/monitor/binding/byMoldNo`
  let reqData = {
    params: {
      serialNumber
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// GET
// /v1/material/{id}
// 获取单个物料信息
export function getMaterialDetail (id) {
  const newUrl = `${baseUrl}/material/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/// v1/cutter/operation_record
export function getOperationRecord ({ pageNum, pageSize, toolCategoryCode }) {
  const newUrl = baseUrl + `/cutter/operation_record`
  const params = {
    pageNum, pageSize, toolCategoryCode
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

// GET
// /v1/mold/{id}/binding_record
// 模具详情绑定记录
export function getBindingRecordMode ({ pageNum, pageSize, id }) {
  const newUrl = baseUrl + `/mold/${id}/binding_record`
  const params = {
    pageNum, pageSize
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
// GET
// /v1/material/minio/download
// 下载模版
export function downloadMaterial () {
  const newUrl = `${baseUrl}/material/minio/download`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// POST
// /v1/material/minio/import
//   批量导入 - minio
export function uploadMaterial (query) {
  const newUrl = `${baseUrl}/material/minio/import`
  return axiosInstance.post(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 工艺路线中相关物料信息
export function getMaterialListProcessInfoId (processInfoId) {
  const newUrl = `${baseUrl}/material_detail/list/${processInfoId}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res.data)
  })
}
export function saveMaterialDetail (params) {
  const newUrl = baseUrl + '/material_detail'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 更新单个物料信息
export function getUpdateMaterialDetail (params) {
  const newUrl = baseUrl + `/material_detail/${params.id}`
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 单个删除
export function MaterialDetailDelete (id) {
  const newUrl = `${baseUrl}/material_detail/${id}`
  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// /v1/material/product_planning/list
// 计划物料消耗清单
export function getMaterialPlanningList ({ orderNumber, planningNumber, productId, pageNum, pageSize, status }) {
  const newUrl = `${basePlanUrl}/material/product_planning/list`
  let reqData = {
    params: {
      pageNum,
      pageSize,
      orderNumber,
      planningNumber,
      status: status[0] === 'all' ? null : status,
      productId
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// POST
// /v1/material/product_planning/list:export
// 计划物料消耗清单
export function exportMaterialPlanning ({ ids, requestId }) {
  const newUrl = `${basePlanUrl}/material/product_planning/list:export`
  const params = {
    ids, requestId
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

// /v1/material/work_order/list
// 工单物料消耗清单
export function getMaterialOrderList ({ deviceId, groupId, orderNumber, planningNumber, productId, workingStepId, pageNum, pageSize, status }) {
  const newUrl = `${basePlanUrl}/material/work_order/list`
  let reqData = {
    params: {
      pageNum,
      pageSize,
      orderNumber,
      planningNumber,
      productId,
      deviceId,
      groupId,
      status: status[0] === 'all' ? null : status,
      workingStepId
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 列举工序列表
export function getProcessOrder (params) {
  const newUrl = `${baseUrl}/working_steps`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

// /v1/material/work_order/list:export
// 工单物料消耗清单导出
export function exportMaterialOrder ({ ids, requestId }) {
  const newUrl = `${basePlanUrl}/material/work_order/list:export`
  const params = {
    ids, requestId
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

//
// /v1/material/excel:export
// 物料定义导出
export function exportMaterialExport ({ requestId }) {
  const newUrl = `${baseUrl}/material/excel:export`
  const params = {
    requestId
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
